package com.ruoyi.common.utils;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.sis.v1.SisClient;
import com.huaweicloud.sdk.sis.v1.model.*;
import com.huaweicloud.sdk.sis.v1.region.SisRegion;

import java.util.HashMap;
import java.util.Map;

public class HuaWeiSISUtils {

    private static final int SLEEP_TIME = 500;
    private static final int MAX_POLLING_NUMS = 1000;

    private static String ak = "HPUAILLXXDHI2IEGEBDE";
    private static String sk = "X2LYM9RdQ43XGF8c0EDKxe8AZlzeb81oQJYi50FC";

    private static String region = "cn-east-3";

    private static SisClient getClient(){
        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        SisClient client = SisClient.newBuilder()
                .withCredential(auth)
                .withRegion(SisRegion.valueOf(region))
                .build();
        return client;
    }

    /**
     * 语音文件识别
     */
    public static String doSubmit(String url){
        SisClient client = getClient();
        PushTranscriberJobsRequest request = new PushTranscriberJobsRequest();
        PostTranscriberJobs body = new PostTranscriberJobs();
        TranscriberConfig configbody = new TranscriberConfig();
        configbody.withProperty(TranscriberConfig.PropertyEnum.fromValue("chinese_8k_common"));
        body.withDataUrl(url);
        body.withConfig(configbody);
        request.withBody(body);
        try {
            PushTranscriberJobsResponse response = client.pushTranscriberJobs(request);
            return response.getJobId();
        } catch (ConnectionException | RequestTimeoutException | ServiceResponseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据生成的任务ID获取录音转文本的结果
     * @param jobId
     */
    public static Map<String,Object> getResult(String jobId){

        int count = 0; // 轮询的次数
        String text = null;
        int audioDuration = 0;

        SisClient client = getClient();
        CollectTranscriberJobRequest request = new CollectTranscriberJobRequest();
        request.withJobId(jobId);
        try {
            while(count<MAX_POLLING_NUMS){
                // 发送请求，获取识别结果
                CollectTranscriberJobResponse response = client.collectTranscriberJob(request);
                // 根据status判断是否识别结束
                // WAITING表示还未结束，FINISHED表示已经结束了
                if(response.getStatus().equals("FINISHED")){
                    // 如果已经结束，从结果中过去segments下result的text属性值
                    text =  response.getSegments().get(0).getResult().getText();
                    audioDuration = response.getAudioDuration();
                    break;
                }
                // 如果没有结束，我们可以通过轮训的方式继续重新获取，直到超时或者有了结果为止
                Thread.sleep(SLEEP_TIME);
                count ++;
            }
        } catch (ConnectionException | RequestTimeoutException | ServiceResponseException | InterruptedException e) {
            e.printStackTrace();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("text",text);
        map.put("audioDuration",audioDuration);
        return map;
    }

}
